//
//  FoodViewController.m
//  CivilianTravel
//
//  Created by 许仕杰 on 16/7/21.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "FoodViewController.h"
#import "HomeFoodModel.h"
#import "HomeFoodTableViewCell.h"
@interface FoodViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic)UITableView *tableView;
/** 内容数组 */
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation FoodViewController

- (NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeFoodTableViewCell" bundle:nil] forCellReuseIdentifier:@"food_cell"];
    [self.view addSubview:self.tableView];
    [self requestFoodWithCityID:self.cityID];
}

- (void)requestFoodWithCityID:(NSString *)cityID{
    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/city/food" parameter:@{@"a_t":@"1469098904000",@"token":@"578624ce19a3f",@"eachpage":@10,@"cityid":cityID,@"sign":@"8cffed2e3811e4d65313fc8a75b63c31fbeff7d6"} method:@"POST" compeleted:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        NSSet *set = dic[@"data"];
        for (NSDictionary *smallDic in set) {
            HomeFoodModel *model = [[HomeFoodModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            NSLog(@"%@",model.name);
            [self.array addObject:model];
        }
        
    } updateUI:^{
        [self.tableView reloadData];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark ---tableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.array.count == 0) {
        return 0;
    }
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeFoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"food_cell" forIndexPath:indexPath];
    HomeFoodModel *model = self.array[indexPath.row];
    [cell setCellWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


@end
